<?php

namespace app\common\business;

use app\common\lib\Arr;
use app\common\model\mysql\SystemCategory;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\Model;

class Build
{
    public $model = null;

    public function __construct()
    {
        $this->model = new \app\common\model\mysql\Build();
    }

    public function getListByWhere($where,$page,$limit,$field='*'){
        $where[] = ['status','=',config("status.mysql.table_normal")];
        if ($page!=0 && $limit !=0){
            $list = $this->model->getPageListByWhere($where,$page,$limit,$field);
        }else{
            $list = $this->model->getNormalListByWhere($where,$field);
        }
        if($list->isEmpty()){
            return [];
        }
        return $list->toArray();
    }

    public function getListByWhereWithOrder($where,$page,$limit,$order='id desc',$field='*'){
        $where[] = ['status','=',config("status.mysql.table_normal")];
        if ($page!=0 && $limit !=0){
            $list = $this->model->getPageListByWhereWithOrder($where,$page,$limit,$order,$field);
        }else{
            $list = $this->model->getNormalListByWhereWithOrder($where,$order,$field);
        }
        if($list->isEmpty()){
            return [];
        }
        return $list->toArray();
    }

    public function getListByWhereWithLimit($where,$limit,$order='id desc',$field='*'){
        $where[] = ['status','=',config("status.mysql.table_normal")];
        $list = $this->model->getNormalListByLimit($where,$limit,$order,$field);
        if($list->isEmpty()){
            return [];
        }
        return $list->toArray();
    }

    public function getCountByWhere($where,$field='*'){
        $where[] = ['status','=',config("status.mysql.table_normal")];
        $list = $this->model->getNormalListByWhere($where,$field);

        return count($list);
    }

    public function calcCountByCategory($list){
        if (empty($list)){
            return [];
        }

        $countList = $this->model->getNormalListByWhere([],'id,category_id')->toArray();
        $arr = Arr::getKeyValueByTwoArray($countList,'','category_id',3);

        foreach ($list as $k=>$v){
            $list[$k]['count'] = $arr[$v['id']] ?? 0;
        }
        return $list;
    }

    /**
     * 获取分页列表
     * @throws Exception
     */
    public function getTableList($page=null,$limit=null,$keyword=''): array
    {
        $where=[
            ['title','like','%'.$keyword.'%']
        ];
        return $this->model->getTableList($where,$page,$limit);
    }

    /**
     * @param $id
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getNormalById($id)
    {
        $data = $this->model->getById($id);
        if (!$data || $data->status != config("status.mysql.table_normal")) {
            return [];
        }
        return $data->toArray();
    }

    /**
     * 新增/保存数据
     * @param $data
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException|Exception
     */
    public function save($data): bool
    {
        if(empty($data['id'])){
            unset($data['id']);
        }
        return $this->model->saveData($data);
    }

    /**
     * 根据id修改状态
     * @param $data
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function updateStatus($data): bool
    {
        if(empty($data['id'])){
            throw new Exception('参数错误');
        }
        return $this->model->updateStatus($data);
    }

    /**
     * 根据id删除
     * @param $id
     * @return bool
     * @throws Exception
     */
    public function deleteById($id): bool
    {
        if(empty($id)){
            throw new Exception('参数错误');
        }
        return $this->model->deleteById($id);
    }

    public function resetInfo($list){
        if (empty($list)){
            return [];
        }

        //字典
        $categoryIdArr = array_column($list,'category_id');
        $mapCategory = [
            ['id','in',$categoryIdArr]
        ];
        $categoryData = (new \app\common\business\Category())->getListByWhere($mapCategory,0,0,'id,name');
        $categoryArr = Arr::getKeyValueByTwoArray($categoryData,'','id',2);

        foreach ($list as $k=>$v){
            $list[$k]['category_text'] = $categoryArr[$v['category_id']] ? $categoryArr[$v['category_id']]['name'] : '';
        }
        return $list;
    }
}
